<?php
/**
 * Class that operate on table 'hs_activity'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsActivityMySqlDAO implements HsActivityDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsActivityMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_activity WHERE activityID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_activity';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_activity ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsActivity primary key
 	 */
	public function delete($activityID){
		$sql = 'DELETE FROM hs_activity WHERE activityID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($activityID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsActivityMySql hsActivity
 	 */
	public function insert($hsActivity){
		$sql = 'INSERT INTO hs_activity (userID, activityTitle, activityWhen, activityWhere, activityStatus, activityDescription, participantLimit, activityCreateTime, visitCount, activityImageCount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsActivity->userID);
		$sqlQuery->set($hsActivity->activityTitle);
		$sqlQuery->set($hsActivity->activityWhen);
		$sqlQuery->set($hsActivity->activityWhere);
		$sqlQuery->set($hsActivity->activityStatus);
		$sqlQuery->set($hsActivity->activityDescription);
		$sqlQuery->setNumber($hsActivity->participantLimit);
		$sqlQuery->set($hsActivity->activityCreateTime);
		$sqlQuery->setNumber($hsActivity->visitCount);
		$sqlQuery->setNumber($hsActivity->activityImageCount);

		$id = $this->executeInsert($sqlQuery);	
		$hsActivity->activityID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsActivityMySql hsActivity
 	 */
	public function update($hsActivity){
		$sql = 'UPDATE hs_activity SET userID = ?, activityTitle = ?, activityWhen = ?, activityWhere = ?, activityStatus = ?, activityDescription = ?, participantLimit = ?, activityCreateTime = ?, visitCount = ?, activityImageCount = ? WHERE activityID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsActivity->userID);
		$sqlQuery->set($hsActivity->activityTitle);
		$sqlQuery->set($hsActivity->activityWhen);
		$sqlQuery->set($hsActivity->activityWhere);
		$sqlQuery->set($hsActivity->activityStatus);
		$sqlQuery->set($hsActivity->activityDescription);
		$sqlQuery->setNumber($hsActivity->participantLimit);
		$sqlQuery->set($hsActivity->activityCreateTime);
		$sqlQuery->setNumber($hsActivity->visitCount);
		$sqlQuery->setNumber($hsActivity->activityImageCount);

		$sqlQuery->set($hsActivity->activityID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_activity';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByUserID($value){
		$sql = 'SELECT * FROM hs_activity WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityTitle($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityTitle = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityWhen($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityWhen = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityWhere($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityWhere = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityStatus($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityDescription($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByParticipantLimit($value){
		$sql = 'SELECT * FROM hs_activity WHERE participantLimit = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityCreateTime($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityCreateTime = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisitCount($value){
		$sql = 'SELECT * FROM hs_activity WHERE visitCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActivityImageCount($value){
		$sql = 'SELECT * FROM hs_activity WHERE activityImageCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByUserID($value){
		$sql = 'DELETE FROM hs_activity WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityTitle($value){
		$sql = 'DELETE FROM hs_activity WHERE activityTitle = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityWhen($value){
		$sql = 'DELETE FROM hs_activity WHERE activityWhen = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityWhere($value){
		$sql = 'DELETE FROM hs_activity WHERE activityWhere = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityStatus($value){
		$sql = 'DELETE FROM hs_activity WHERE activityStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityDescription($value){
		$sql = 'DELETE FROM hs_activity WHERE activityDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByParticipantLimit($value){
		$sql = 'DELETE FROM hs_activity WHERE participantLimit = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityCreateTime($value){
		$sql = 'DELETE FROM hs_activity WHERE activityCreateTime = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisitCount($value){
		$sql = 'DELETE FROM hs_activity WHERE visitCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActivityImageCount($value){
		$sql = 'DELETE FROM hs_activity WHERE activityImageCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsActivityMySql 
	 */
	protected function readRow($row){
		$hsActivity = new HsActivity();
		
		$hsActivity->activityID = $row['activityID'];
		$hsActivity->userID = $row['userID'];
		$hsActivity->activityTitle = $row['activityTitle'];
		$hsActivity->activityWhen = $row['activityWhen'];
		$hsActivity->activityWhere = $row['activityWhere'];
		$hsActivity->activityStatus = $row['activityStatus'];
		$hsActivity->activityDescription = $row['activityDescription'];
		$hsActivity->participantLimit = $row['participantLimit'];
		$hsActivity->activityCreateTime = $row['activityCreateTime'];
		$hsActivity->visitCount = $row['visitCount'];
		$hsActivity->activityImageCount = $row['activityImageCount'];

		return $hsActivity;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsActivityMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>